package com.spzx.product.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.spzx.common.core.utils.StringUtils;
import com.spzx.common.core.web.domain.AjaxResult;
import com.spzx.common.core.web.page.TableDataInfo;
import com.spzx.product.domain.ProductUnit;
import com.spzx.product.service.ProductUnitService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.aspectj.weaver.loadtime.Aj;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.spzx.common.core.web.controller.BaseController;

import java.util.List;

/**
 * <p>
 * 商品单位 前端控制器
 * </p>
 *
 * @author atguigu
 * @since 2024-10-26
 */
@Tag(name = "商品单位")
@RestController
@RequestMapping("/productUnit")
public class ProductUnitController extends BaseController {

    @Autowired
    private ProductUnitService productUnitService;

    @Operation(description = "分页查询及带条件查询")
    @GetMapping("/list")
    public TableDataInfo getPageInfo(ProductUnit productUnit){
        LambdaQueryWrapper<ProductUnit> productUnitLambdaQueryWrapper = new LambdaQueryWrapper<>();
        productUnitLambdaQueryWrapper.like(StringUtils.hasText(productUnit.getName()),ProductUnit::getName,productUnit.getName());
        List<ProductUnit> productUnitList = productUnitService.list(productUnitLambdaQueryWrapper);
        return getDataTable(productUnitList);
    }

    @Operation(description = "新增商品单位")
    @PostMapping
    public AjaxResult saveProductUnit(@RequestBody ProductUnit productUnit){
        /*productUnitService.list().forEach(ProductUnit -> {
        });
        if (){
            throw new serviceException
        }*/
        boolean save = productUnitService.save(productUnit);
        return toAjax(save);

    }

    @Operation(description = "根据id查询商品单位")
    @GetMapping("/{id}")
    public AjaxResult getProductUnit(@PathVariable Long id){
        ProductUnit byId = productUnitService.getById(id);
        return AjaxResult.success(byId);
    }

    @Operation(description = "根据Id修改商品单位")
    @PutMapping
    public AjaxResult updateProductUnit(@RequestBody ProductUnit productUnit){
        boolean save = productUnitService.saveOrUpdate(productUnit);
        return toAjax(save);
    }

    @Operation(description = "根据Id删除商品单位")
    @DeleteMapping("/{ids}")
    public AjaxResult deleteProductUnit(@PathVariable List<Long> ids){
        boolean byIds = productUnitService.removeBatchByIds(ids);
        return toAjax(byIds);
    }

    @Operation(summary = "查询所有单位信息")
    @GetMapping("/getUnitAll")
    public AjaxResult getBrandAll(){
        List<ProductUnit> list = productUnitService.list();
        return success(list);
    }

}
